package HelperFinancial.User;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import HelperFinancial.Class.PieDetailsItem;
import HelperFinancial.Class.View_PieChart;
import HelperFinancial.DataBase.ConfigurationAdapter;
import HelperFinancial.DataBase.GraphicDataAdapter;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;


public class GraphicActivity extends Activity {

	List<PieDetailsItem> PieData = new ArrayList<PieDetailsItem>(0);
	GraphicDataAdapter adapter = new GraphicDataAdapter(this);
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.graphic);
        //------------------------------------------------------------------------------------------
        // Used vars declaration
        //------------------------------------------------------------------------------------------
        PieDetailsItem Item;
        Random mNumGen  = new Random();
        int MaxPieItems = adapter.getAllValidTypesOfDebits().size();
        int MaxCount  = 0;
        int ItemCount = 0;
        //------------------------------------------------------------------------------------------
        // Generating data by a random loop
        //------------------------------------------------------------------------------------------
        List<Integer> valores = adapter.getValues();
        Map<Integer, Integer> mapColor = new HashMap<Integer, Integer>();
        for (int i = 0; i < valores.size() ; i++) {
        	ItemCount  = valores.get(i);//mNumGen.nextInt(256);
        	Item       = new PieDetailsItem();
        	Item.Count = ItemCount;
        	Item.Label = "Valeur " + mNumGen.nextInt(1000);
        	Item.Color = 0xff000000 + 256*256*mNumGen.nextInt(256) + 256*mNumGen.nextInt(256) + mNumGen.nextInt(256);
        	PieData.add(Item);
        	MaxCount += ItemCount;
        	mapColor.put(i, Item.Color);
        }
        //------------------------------------------------------------------------------------------
        // OverlayId  => Image to be drawn on top of pie to make it more beautiful!
        //------------------------------------------------------------------------------------------
        int OverlayId = R.drawable.cam_overlay_big;
        //------------------------------------------------------------------------------------------
        // Size => Pie size
        //------------------------------------------------------------------------------------------
        int Size = 150;
        //------------------------------------------------------------------------------------------
        // BgColor  => The background Pie Color
        //------------------------------------------------------------------------------------------
        int BgColor = 0;
        //------------------------------------------------------------------------------------------
        // mBackgroundImage  => Temporary image will be drawn with the content of pie view
        //------------------------------------------------------------------------------------------
        Bitmap mBackgroundImage = Bitmap.createBitmap(Size, Size, Bitmap.Config.RGB_565);
        //------------------------------------------------------------------------------------------
        // Generating Pie view
        //------------------------------------------------------------------------------------------
        View_PieChart PieChartView = new View_PieChart( this );
        PieChartView.setLayoutParams(new LayoutParams(Size, Size));
        PieChartView.setGeometry(Size, Size, 2, 2, 2, 2, OverlayId);
        PieChartView.setSkinParams(BgColor);
        PieChartView.setData(PieData, MaxCount);
        PieChartView.invalidate();
        //------------------------------------------------------------------------------------------
        // Draw Pie Vien on Bitmap canvas
        //------------------------------------------------------------------------------------------
        PieChartView.draw(new Canvas(mBackgroundImage));
        PieChartView = null;
        //------------------------------------------------------------------------------------------
        // Create a new ImageView to add to main layout
        //------------------------------------------------------------------------------------------
        ImageView mImageView = new ImageView(this);
	    mImageView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
	    mImageView.setBackgroundColor(BgColor);
	    mImageView.setImageBitmap( mBackgroundImage );
	    //------------------------------------------------------------------------------------------
        // Finaly add Image View to target view !!!
        //------------------------------------------------------------------------------------------
        LinearLayout TargetPieView =  (LinearLayout) findViewById(R.id.pie_container);
	    TargetPieView.addView(mImageView);
	    
	    LinearLayout legendName =  (LinearLayout) findViewById(R.id.legend_name);
	    List<String> validTypes = adapter.getAllValidTypesOfDebits();
	    Map<String, Double> values = adapter.getMapTypeToPercent();
	    for (int j = 0; j < validTypes.size(); j++) {
	    	TableRow table = new TableRow(this);
	    	
	    	TextView legend =  new TextView(this);
			legend.append(validTypes.get(j));
			legend.setTextColor(mapColor.get(j));
			table.addView(legend);
			
			
			TextView percent = new TextView(this);
			percent.setGravity(0x05);
			percent.append(String.format("%.1f", values.get(validTypes.get(j))) + "%");
			percent.setTextColor(mapColor.get(j));
			table.addView(percent);
			legendName.addView(table);
		}
    }
    
    @Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu_configuration, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getItemId() == R.id.filter) {
			finish();
			startActivity(new Intent(this, DateUpdateForGraphicsActivity.class));
			return true;
		}

		if (item.getItemId() == R.id.redefine_default) {
			redefinirPadrao();
			return true;
		}
		return (super.onOptionsItemSelected(item));
	}
	
	private void redefinirPadrao() {
		ConfigurationAdapter config = ConfigurationAdapter.getInstance(this);
		config.populate();
		finish();
		startActivity(new Intent(this, GraphicActivity.class));

	}
}